package tech.lq0.mineapex.entity;

import io.netty.buffer.Unpooled;
import tech.lq0.mineapex.registry.EntityRegistry;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.projectile.AbstractArrowEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.IPacket;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.IndirectEntityDamageSource;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

public class BocekArrowEntity extends AbstractArrowEntity {

    public BocekArrowEntity(EntityType<? extends AbstractArrowEntity> type, World worldIn) {
        super(type, worldIn);
    }

    public BocekArrowEntity(EntityType<? extends AbstractArrowEntity> type, double x, double y, double z, World worldIn) {
        super(type, x, y, z, worldIn);
    }

    public BocekArrowEntity(EntityType<? extends AbstractArrowEntity> type, LivingEntity shooter, World worldIn) {
        super(type, shooter, worldIn);
    }

    public BocekArrowEntity(World world, LivingEntity entity) {
        super(EntityRegistry.ARROW_ENTITY.get(), entity, world);
    }

    @Override
    public void tick() {
        super.tick();
        this.setMotion(getMotion().x * 1.01F, getMotion().y - (double) 0.02F, getMotion().z * 1.01F);
        if (this.ticksExisted >= 600) {
            this.remove();
        }
    }

    @Override
    @ParametersAreNonnullByDefault
    protected void onEntityHit(EntityRayTraceResult result) {
        Entity entity = result.getEntity();
        entity.attackEntityFrom(new IndirectEntityDamageSource("mineapex_arrow", this, this.getShooter()), (float) this.getDamage());
        this.remove();
    }

    @Override
    @Nonnull
    public IPacket<?> createSpawnPacket() {
        PacketBuffer pack = new PacketBuffer(Unpooled.buffer());
        pack.writeDouble(getPosX());
        pack.writeDouble(getPosY());
        pack.writeDouble(getPosZ());
        pack.writeInt(getEntityId());
        pack.writeUniqueId(getUniqueID());

        return NetworkHooks.getEntitySpawningPacket(this);
    }

    @Override
    public boolean hasNoGravity() {
        return true;
    }

    @Override
    @Nonnull
    protected ItemStack getArrowStack() {
        return Items.ARROW.getDefaultInstance();
    }
}
